//SimpleLogin.js

let path="./plugins/SimpleLogin/";

function register(player)
{
    let win=window.getCustomWindowBuilder("登录系统 - 注册");
    win.buildInput("设置密码","输入密码");
    win.buildInput("重复密码","输入密码");
    win.showToPlayer(player,"registerCallback");
}

function registerCallback(evt)
{
    player=evt.getPlayer();
    pswd1=window.getEventCustomVar(evt,0,"input");
    pswd2=window.getEventCustomVar(evt,1,"input");
    logger.info(pswd1);
    logger.info(pswd2);
    if(pswd1!==pswd2)
    {
        player.sendMessage("§4两次密码输入不一致，请重新输入！");
        register(player);
    }
    else
    {
        let ip=player.getAddress();
        let config={"passworld":pswd1,"lastaddress":ip};
        let lgnp=JSON.parse(manager.readFile(path+"OnlinePlayers.yml"));
        lgnp[player.getName()]="true";
        manager.writeFile(path+"OnlinePlayers.yml",JSON.stringify(lgnp));
        manager.writeFile(path+player.getName()+".json",JSON.stringify(config));
        player.sendMessage("§a注册成功，欢迎！");
    }
    
}

function login(player)
{
    let win=window.getCustomWindowBuilder("登录系统 - 登录");
    win.buildInput("密码","输入密码");
    win.showToPlayer(player,"loginCallback");
}

function loginCallback(evt)
{
    let player=evt.getPlayer();
    let inputpswd=window.getEventCustomVar(evt,0,"input");
    logger.info(inputpswd);
    let pswd=JSON.parse(manager.readFile(path+player.getName()+".json")).passworld;
    if(pswd===inputpswd)
    {
        let ip=player.getAddress();
        let config={"passworld":pswd,"lastaddress":ip};
        let lgnp=JSON.parse(manager.readFile(path+"OnlinePlayers.yml"));
        lgnp[player.getName()]="true";
        manager.writeFile(path+"OnlinePlayers.yml",JSON.stringify(lgnp));
        manager.writeFile(path+player.getName()+".json",JSON.stringify(config));
        player.sendMessage("§a登录成功，欢迎您，"+player.getName());
    }
}

function PlayerFormRespondedEvent(evt)
{
    let player=evt.getPlayer()
    if(PlayerLoginStauteCheck(player)===false)
    {
        if(manager.readFile(path+player.getName()+".json")==="FILE NOT FOUND")
            register(player);
        else
            login(player);
    }
}

function PlayerLoginStauteCheck(player)
{
    let lgnp=JSON.parse(manager.readFile(path+"OnlinePlayers.yml"));
    if(lgnp[player.getName()]==="true")
        return true;
    else
        return false;
}

function PlayerMoveEvent(evt)
{
    let player=evt.getPlayer();
    let pos=evt.getFrom();
    if(PlayerLoginStauteCheck(player)===false)
    {
        player.teleport(pos);
        player.sendMessage("§4请先登录！！！");
    }
}

function InventoryOpenEvent(evt)
{
    let player=evt.getPlayer();
    if(PlayerLoginStauteCheck(player)===false)
    {
        evt.setCancelled(true);
        player.sendMessage("§4请先登录！！！");
    }
}

function BlockPlaceEvent(evt)
{
    let player=evt.getPlayer();
    if(PlayerLoginStauteCheck(player)===false)
    {
        evt.setCancelled(true);
        player.sendMessage("§4请先登录！！！");
    }
}

function BlockBreakEvent(evt)
{
    let player=evt.getPlayer();
    if(PlayerLoginStauteCheck(player)===false)
    {
        evt.setCancelled(true);
        player.sendMessage("§4请先登录！！！");
    }
}

function PluginEnableEvent(evt)
{
    logger.info("SimpleLogin已加载");
    manager.writeFile(path+"OnlinePlayers.yml",JSON.stringify({}));
    logger.info("临时在线玩家数据库已建立");
}

function PluginDisableEvent(evt)
{
    logger.info("SimpleLogin已卸载");
    manager.deleteFile(path+"OnlinePlayers.yml");
    logger.info("临时在线玩家数据库已删除");
}

function PlayerLocallyInitializedEvent(evt)
{
    let player=evt.getPlayer();
    if(manager.readFile(path+player.getName()+".json")==="FILE NOT FOUND")
        register(player);
    else
    {
        let ip=JSON.parse(manager.readFile(path+player.getName()+".json")).lastaddress;
        if(player.getAddress()===ip)
        {
            let lgnp=JSON.parse(manager.readFile(path+"OnlinePlayers.yml"));
            lgnp[player.getName()]="true";
            manager.writeFile(path+"OnlinePlayers.yml",JSON.stringify(lgnp));
            player.sendMessage("§a检测到您本次登录ip与上次相同，已为你自动登录");
        }
        else
            login(player);
    }
}

function PlayerQuitEvent(evt)
{
    let player=evt.getPlayer();
    let lgnp=JSON.parse(manager.readFile(path+"OnlinePlayers.yml"));
    lgnp[player]="false";
}